package com.easyfun.healthmagicbox;

import java.util.ArrayList;
import java.util.HashMap;

import com.easyfun.healthmagicbox.base.BaseActivity;
import com.zxing.activity.CaptureActivity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnCreateContextMenuListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

public class SubMedicalUsageActivity extends BaseActivity {

	private TextView resultTextView;
	SimpleAdapter inlistItemAdapter;
	SimpleAdapter listItemAdapter;
	
	ArrayList<HashMap<String, Object>> listItem;
	ArrayList<HashMap<String, Object>> inlistItem;
	class medical {
		String medicalname;
		String medicalfactoryname;

		public medical(String a, String b) {
			medicalname = a;
			medicalfactoryname = b;
		}
	}

	private final medical[] medical = { new medical("咳嗽糖浆", "双鹤药业"),
			new medical("二价双花", "三金制药") };
	private final medical[] medicalin = { new medical("双黄连", "双鹤药业"),
			new medical("达克宁", "三金制药") };

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.medicallayout);
		if (!checkPersonId())
			return;
		resultTextView = (TextView) this.findViewById(R.id.tv_scan_result);
		// qrStrEditText = (EditText) this.findViewById(R.id.et_qr_string);
		// qrImgImageView = (ImageView) this.findViewById(R.id.iv_qr_image);

		Button scanBarCodeButton = (Button) this
				.findViewById(R.id.btn_scan_barcode);
		scanBarCodeButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// 打开扫描界面扫描条形码或二维码
				Intent openCameraIntent = new Intent(
						SubMedicalUsageActivity.this, CaptureActivity.class);
				startActivityForResult(openCameraIntent, 0);
			}
		});

		setupMedicalInListView();
		setupMedicalListView();
		// Button generateQRCodeButton = (Button)
		// this.findViewById(R.id.btn_add_qrcode);
		/*
		 * generateQRCodeButton.setOnClickListener(new OnClickListener() {
		 * 
		 * 
		 * @Override public void onClick(View v) { try { String contentString =
		 * qrStrEditText.getText().toString(); if (!contentString.equals("")) {
		 * //根据字符串生成二维码图片并显示在界面上，第二个参数为图片的大小（350*350） Bitmap qrCodeBitmap =
		 * EncodingHandler.createQRCode(contentString, 350);
		 * qrImgImageView.setImageBitmap(qrCodeBitmap); }else {
		 * Toast.makeText(BarCodeTestActivity.this, "Text can not be empty",
		 * Toast.LENGTH_SHORT).show(); }
		 * 
		 * } catch (WriterException e) { // TODO Auto-generated catch block
		 * e.printStackTrace(); } } });
		 */

	}

	private void setupMedicalListView() {
		// 绑定Layout里面的ListView
		ListView list = (ListView) findViewById(R.id.medicallist);

		// 生成动态数组，加入数据
		listItem = new ArrayList<HashMap<String, Object>>();

		for (int i = 0; i < medical.length; i++) {
			HashMap<String, Object> map = new HashMap<String, Object>();

			map.put("ItemTitle", medical[i].medicalname);
			map.put("ItemText", medical[i].medicalfactoryname);
			listItem.add(map);
		}
		// 生成适配器的Item和动态数组对应的元素
		listItemAdapter = new SimpleAdapter(this, listItem,// 数据源
				R.layout.medicallistitem,// ListItem的XML实现
				// 动态数组与ImageItem对应的子项
				new String[] { "ItemTitle", "ItemText" },
				// ImageItem的XML文件里面的一个ImageView,两个TextView ID
				new int[] { R.id.ItemTitle, R.id.ItemText });

		// 添加并且显示
		list.setAdapter(listItemAdapter);

		// 添加点击
		list.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				
				setTitle("点击第" + arg2 + "个项目");
				new AlertDialog.Builder(SubMedicalUsageActivity.this)   
				.setTitle("添加")  
				.setMessage("三金双黄连，每日四次，一次一片")  
				.setPositiveButton("是", new DialogInterface.OnClickListener() {  
                    public void onClick(DialogInterface dialog, int which) {  
						addMedicalinItem(); 
					}  
					})  
				.setNegativeButton("否", null)  
				.show();
			}
		});

		/*
		// 添加长按点击
		list.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {

			@Override
			public void onCreateContextMenu(ContextMenu menu, View v,
					ContextMenu.ContextMenuInfo menuInfo) {
				menu.setHeaderTitle("长按菜单-ContextMenu");
				menu.add(0, 0, 0, "弹出长按菜单0");
				menu.add(0, 1, 0, "弹出长按菜单1");
			}
		});
		*/
	}
	
	private void addMedicalItem()  
	{  
		HashMap<String, Object> map = new HashMap<String, Object>();  
		 
		map.put("title", "标题");  
		map.put("text", "要显示的内容");  
		listItem.add(map);  
		listItemAdapter.notifyDataSetChanged();  
	}  
	  
	private void deleteMedicalItem()  
	{  
		int size = listItem.size();  
		if( size > 0 )  
		{  
			listItem.remove(listItem.size() - 1);  
			listItemAdapter.notifyDataSetChanged();  
		}  
	}  
	
	private void addMedicalinItem()  
	{  
		HashMap<String, Object> map = new HashMap<String, Object>();  
		 
		map.put("title", "标题");  
		map.put("text", "要显示的内容");  
		inlistItem.add(map);  
		inlistItemAdapter.notifyDataSetChanged();  
	}  
	  
	private void deleteMedicalinItem()  
	{  
		int size = listItem.size();  
		if( size > 0 )  
		{  
			inlistItem.remove(listItem.size() - 1);  
			inlistItemAdapter.notifyDataSetChanged();  
		}  
	} 
	
	private void setupMedicalInListView() {
		// --------------------------------------------------------------
		// 绑定Layout里面的ListView -------------------------------------------
		ListView inlist = (ListView) findViewById(R.id.medicalinlist);

		// 生成动态数组，加入数据
		inlistItem = new ArrayList<HashMap<String, Object>>();

		for (int i = 0; i < medicalin.length; i++) {
			HashMap<String, Object> map = new HashMap<String, Object>();

			map.put("ItemTitle", medicalin[i].medicalname);
			map.put("ItemText", medicalin[i].medicalfactoryname);
			inlistItem.add(map);
		}
		// 生成适配器的Item和动态数组对应的元素
		inlistItemAdapter = new SimpleAdapter(this, inlistItem,// 数据源
				R.layout.medicallistitem,// ListItem的XML实现
				// 动态数组与ImageItem对应的子项
				new String[] { "ItemTitle", "ItemText" },
				// ImageItem的XML文件里面的一个ImageView,两个TextView ID
				new int[] { R.id.ItemTitle, R.id.ItemText });
		// 添加并且显示
		inlist.setAdapter(inlistItemAdapter);
		

		// 添加点击
		inlist.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				setTitle("点击第" + arg2 + "个项目");
				new AlertDialog.Builder(SubMedicalUsageActivity.this)   
				.setTitle("删除")  
				.setMessage("三金双黄连，每日四次，一次一片")  
				.setPositiveButton("是", new DialogInterface.OnClickListener() {  
                    public void onClick(DialogInterface dialog, int which) {  
						deleteMedicalinItem(); 
					}  
					})  
				.setNegativeButton("否", null)  
				.show();
				
				inlistItemAdapter.notifyDataSetChanged();
			}
		});

		/*
		// 添加长按点击
		inlist.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {

			@Override
			public void onCreateContextMenu(ContextMenu menu, View v,
					ContextMenu.ContextMenuInfo menuInfo) {
				menu.setHeaderTitle("长按菜单-ContextMenu");
				menu.add(0, 0, 0, "弹出长按菜单0");
				menu.add(0, 1, 0, "弹出长按菜单1");
			}
		});
		*/
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		// 处理扫描结果（在界面上显示）
		if (resultCode == RESULT_OK) {
			Bundle bundle = data.getExtras();
			String scanResult = bundle.getString("result");
			resultTextView.setText(scanResult);
		}
	}

}